/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.autoconfigure.oss.impl;

import io.minio.MinioClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.moon.framework.autoconfigure.exception.domain.MoonException;
import org.moon.framework.autoconfigure.oss.OssService;
import org.moon.framework.autoconfigure.oss.domain.MoonFile;
import org.moon.framework.autoconfigure.oss.props.OssProperties;
import org.moon.framework.autoconfigure.oss.utils.OssUtils;
import org.moon.framework.autoconfigure.springmvc.response.RCode;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * minio 服务
 * @author moon
 */
@Slf4j
public class MinioOssProviderImpl implements OssService {

    private MinioClient client;
    private OssProperties ossProperties;

    @SneakyThrows
    public MinioOssProviderImpl(OssProperties ossProperties){
        String [] up = ossProperties.getEndpoint().split(":");
        this.client = new MinioClient("http://"+up[0],Integer.valueOf(up[1]), ossProperties.getAccessKey(), ossProperties.getSecretKey());
        this.ossProperties = ossProperties;
    }

    @Override
    public MoonFile putFile(MultipartFile file) {
        return putFile(file.getOriginalFilename(),file);
    }

    @Override
    public MoonFile putFile(String fileName, MultipartFile file) {
        try {
            String bucketName = OssUtils.getBucketName(ossProperties.getBucketName());
            if (!this.client.bucketExists(bucketName)) {
                this.client.makeBucket(bucketName);
//                this.client.setBucketPolicy(bucketName, getPolicyType(this.getBucketName(bucketName), PolicyType.READ));
            }
            String originalName = fileName;
            fileName = OssUtils.getFileName(fileName);
            InputStream stream = file.getInputStream();
            this.client.putObject(bucketName, fileName, stream, (long)stream.available(), null, null, "application/octet-stream");
            MoonFile moonFile = MoonFile.builder().originalName(originalName).name(fileName).domain(this.ossProperties.getEndpoint()).link(bucketName.concat("/").concat(fileName)).build();
            return moonFile;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new MoonException(RCode.FAIL.getCode(),"上传文件失败",e);
        }
    }

    @Override
    public void removeFiles(List<String> fileNames) {
        try {
            this.client.removeObjects(OssUtils.getBucketName(ossProperties.getBucketName()), fileNames);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new MoonException(RCode.FAIL.getCode(),"删除文件失败",e);
        }
    }

    @Override
    public void removeFile(String fileName) {
        try {
            this.client.removeObject(OssUtils.getBucketName(ossProperties.getBucketName()), fileName);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new MoonException(RCode.FAIL.getCode(),"删除文件失败",e);
        }
    }
}
